package org.jeecg.modules.music.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.PermissionData;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.base.service.BaseCommonService;
import org.jeecg.modules.music.entity.MusSong;
import org.jeecg.modules.music.entity.MusSong;
import org.jeecg.modules.music.entity.MusSong;
import org.jeecg.modules.music.service.IMusSongService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Date;

@Slf4j
@Api(tags="歌曲管理")
@RestController
@RequestMapping("/mus/song")
public class MusSongController {

    @Resource
    private BaseCommonService baseCommonService;

    @Autowired
    private IMusSongService musSongService;

    @Value("${jeecg.path.upload}")
    private String upLoadPath;

    @ApiOperation("添加")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Result<MusSong> add(@RequestBody JSONObject jsonObject){
        Result<MusSong> result = new Result<MusSong>();

        try{
            MusSong song = JSON.parseObject(jsonObject.toJSONString(),MusSong.class);

            QueryWrapper<MusSong> queryWrapper = new QueryWrapper<>();
            if(song.getSongName()!=null){
                queryWrapper.eq("song_name",song.getSongName());
            }
            MusSong entity = musSongService.getOne(queryWrapper);
            if(entity==null){
                song.setCreateTime(new Date());
                song.setDelFlag(CommonConstant.DEL_FLAG_0);
                musSongService.saveSong(song);
                result.success("添加成功！");
            }

        }catch (Exception e){
            log.error(e.getMessage(), e);
            result.error500("操作失败");
        }
        return result;
    }

    @ApiOperation("列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Result<IPage<MusSong>> queryPageList(MusSong artist, @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                                  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize, HttpServletRequest req) {
        Result<IPage<MusSong>> result = new Result<IPage<MusSong>>();
        QueryWrapper<MusSong> queryWrapper = QueryGenerator.initQueryWrapper(artist, req.getParameterMap());
        Page<MusSong> page = new Page<MusSong>(pageNo, pageSize);
        IPage<MusSong> pageList = musSongService.page(page,queryWrapper);
        result.setSuccess(true);
        result.setResult(pageList);

        return result;
    }

    @ApiOperation("删除")
    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    public Result<?> delete(@RequestParam(name="id",required=true) String id) {
        baseCommonService.addLog("删除歌曲，id： " +id ,CommonConstant.LOG_TYPE_2, 3);
        this.musSongService.deleteSong(id);
        return Result.ok("删除成功");
    }

    @RequestMapping(value = "/deleteBatch", method = RequestMethod.DELETE)
    public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
        baseCommonService.addLog("批量删除， ids： " +ids ,CommonConstant.LOG_TYPE_2, 3);
        this.musSongService.deleteBatchSongs(ids);
        return Result.ok("批量删除歌曲成功");
    }

    @ApiOperation("编辑")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
    public Result<MusSong> edit(@RequestBody JSONObject jsonObject) {
        Result<MusSong> result = new Result<MusSong>();
        try {
            MusSong song = musSongService.getById(jsonObject.getString("id"));
            baseCommonService.addLog("编辑歌曲，id： " +jsonObject.getString("id") ,CommonConstant.LOG_TYPE_2, 2);
            if(song==null) {
                result.error500("未找到对应实体");
            }else {
                MusSong musSong = JSON.parseObject(jsonObject.toJSONString(), MusSong.class);
                song.setUpdateTime(new Date());
                musSongService.editSong(musSong);
                result.success("修改成功!");
            }
        }catch (Exception e){
            log.error(e.getMessage(), e);
            result.error500("操作失败");
        }
        return result;
    }
}
